﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Test
{
    public class ServiceAdapter
    {
        static ServiceAdapter()
        {
            Inventory.InventoryChangedEvent += new EventHandler<InventoryArgs>(Inventory_InventoryChangedEvent);
            Inventory.OutofStockEvent += new EventHandler<InventoryArgs>(Inventory_OutofStockEvent);
            Inventory.InventoryAddedEvent += new EventHandler<InventoryArgs>(Inventory_InventoryAddedEvent);
        }

        static void Inventory_InventoryAddedEvent(object sender, InventoryArgs e)
        {
            ServiceBus.Contract.Message message = new ServiceBus.Contract.Message();
            message.Sender = e.Operator;
            message.PrimaryID = e.Inventory.ProductID.ToString();
            message.Title = e.ChangedNumber.ToString();

            ServiceBus.Service.MessageBox box = new ServiceBus.Service.MessageBox("NewInventory");
            box.MessageEnable = true;
            box.EmailEnable = false;
            box.DatabaseEnable = false;
            box.SendMessage(message);
        }

        static void Inventory_OutofStockEvent(object sender, InventoryArgs e)
        {
            ServiceBus.Contract.Message message = new ServiceBus.Contract.Message();
            message.Sender = e.Operator;
            message.PrimaryID = e.Inventory.ProductID.ToString();
            message.Title = e.ChangedNumber.ToString();

            ServiceBus.Service.MessageBox box = new ServiceBus.Service.MessageBox("OutofStock");
            box.MessageEnable = true;
            box.EmailEnable = false;
            box.DatabaseEnable = false;
            box.SendMessage(message);
        }

        static void Inventory_InventoryChangedEvent(object sender, InventoryArgs e)
        {
            ServiceBus.Contract.Message message = new ServiceBus.Contract.Message();
            message.Sender = e.Operator;
            message.PrimaryID = e.Inventory.ProductID.ToString();
            message.Title = e.ChangedNumber.ToString();

            ServiceBus.Service.MessageBox box = new ServiceBus.Service.MessageBox("InventoryChange");
            box.MessageEnable = true;
            box.EmailEnable = false;
            box.DatabaseEnable = false;
            box.SendMessage(message);
        }
        public static Inventory GetInventory(int productid)
        {
            return Inventory.GetInventory(productid);
        }
        public static Inventory[] GetInventoryList()
        {
            return Inventory.GetInventoryList();
        }
        public static bool ChangeInventory(string user, int productid, int changednumber)
        {
            Inventory inv = Inventory.GetInventory(productid);
            return inv.ChangeInventory(user, changednumber);
        }
        public static int CreateNewProduct(Inventory inventory, string user)
        {
            return new Inventory(user,inventory.ProductName,inventory.CurrentInventory).ProductID;
        }
    }
}
